package thesis.filesharing.dao.impl;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.springframework.stereotype.Repository;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import thesis.filesharing.common.exception.system.AppSystemException;
import thesis.filesharing.dao.XMLDatasourceDAO;
import thesis.filesharing.resources.Constants;

@Repository
public class XMLDatasourceDAOImpl implements XMLDatasourceDAO {

	@Override
	public String getCurrentSemester() {
		String fileName = Constants.SEMESTER_FILE_PATH;
		String tagName = "semester";
		String attributeName = "current";
		
		try {
			File file = new File(fileName);
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder documentBuilder = factory.newDocumentBuilder();
			Document doc = documentBuilder.parse(file);
			doc.getDocumentElement().normalize();
			NodeList nodeList = doc.getElementsByTagName(tagName);
			
			Node node = null;
			for (int i = 0; i < nodeList.getLength(); i++) {
				node = nodeList.item(i);
				Element e = (Element) node;
				if ("true".equalsIgnoreCase(e.getAttribute(attributeName))) {
					return e.getTextContent();
				}
			}
			return null;
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new AppSystemException(ex);
		}
	}

	@Override
	public void changeCurrentSemester(String semester) {
		String fileName = Constants.SEMESTER_FILE_PATH;
		String tagName = "semester";
		String attributeName = "current";
		
		try {
			File file = new File(fileName);
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder documentBuilder = factory.newDocumentBuilder();
			Document doc = documentBuilder.parse(file);
			doc.getDocumentElement().normalize();
			NodeList nodeList = doc.getElementsByTagName(tagName);
			
			Node node = null;
			for (int i = 0; i < nodeList.getLength(); i++) {
				node = nodeList.item(i);
				Element e = (Element) node;
				if ("true".equalsIgnoreCase(e.getAttribute(attributeName))) {
					if (semester.equalsIgnoreCase(e.getTextContent())) {
						return;
					} else {
						e.setAttribute(tagName, "false");
					}
				}
			}
			
			for (int i = 0; i < nodeList.getLength(); i++) {
				node = nodeList.item(i);
				Element e = (Element) node;
				if (semester.equalsIgnoreCase(e.getTextContent())) {
					e.setAttribute(tagName, "true");
				}
			}
			
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(new File(fileName));
			transformer.transform(source, result);
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new AppSystemException(ex);
		}
	}
}
